package com.proton.carepatchtemp.utils;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.application.VCare;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by MoonlightSW on 2016/12/7.
 */

public class FormatUtils {

    private static final long seconds_of_1minute = 60;

    private static final long seconds_of_30minutes = 30 * 60;

    private static final long seconds_of_1hour = 60 * 60;

    private static final long seconds_of_1day = 24 * 60 * 60;

    private static final long seconds_of_2day = seconds_of_1day * 2;

    private static final long seconds_of_15days = seconds_of_1day * 15;

    private static final long seconds_of_30days = seconds_of_1day * 30;

    private static final long seconds_of_6months = seconds_of_30days * 6;

    private static final long seconds_of_1year = seconds_of_30days * 12;


    /**
     * @return timtPoint距离现在经过的时间，分为
     * 刚刚，1-29分钟前，半小时前，1-23小时前，1-14天前，半个月前，1-5个月前，半年前，1-xxx年前
     */
    public static String getTimeElapse(long createTime) {

        long nowTime = System.currentTimeMillis() / 1000;

        //createTime是发表文章的时间

        long oldTime = createTime / 1000;

        //elapsedTime是发表和现在的间隔时间

        long elapsedTime = nowTime - oldTime;

        Calendar calendar0 = Calendar.getInstance();
        calendar0.setTimeInMillis(System.currentTimeMillis());

        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTimeInMillis(createTime);

        if (elapsedTime < seconds_of_1minute) {
            return VCare.get().getString(R.string.string_just);
        }
        if (elapsedTime < seconds_of_1hour) {
            return elapsedTime / seconds_of_1minute + VCare.get().getString(R.string.string_minute_ago);
        }

        if (elapsedTime < seconds_of_1day && calendar0.get(Calendar.DAY_OF_MONTH) == calendar1.get(Calendar.DAY_OF_MONTH)) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");//初始化Formatter的转换格式。
            return VCare.get().getString(R.string.string_today) + " " + formatter.format(createTime).split(" ")[1];
        }


        if (elapsedTime < seconds_of_1day && calendar0.get(Calendar.DAY_OF_MONTH) != calendar1.get(Calendar.DAY_OF_MONTH)) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");//初始化Formatter的转换格式。
            return VCare.get().getString(R.string.string_yestoday) + " " + formatter.format(createTime).split(" ")[1];
        }

        if (elapsedTime < seconds_of_2day) {
            SimpleDateFormat formatter0 = new SimpleDateFormat("yyyy-MM-dd HH:mm");//初始化Formatter的转换格式。
            return VCare.get().getString(R.string.string_yestoday) + " " + formatter0.format(createTime).split(" ")[1];
        }

        //同一年的只返回几月几号几点几分
        if (calendar0.get(Calendar.YEAR) == calendar1.get(Calendar.YEAR)) {
            return transferLongToDate(VCare.get().getString(R.string.string_month_day_h_m), createTime);
        }

        if (calendar0.get(Calendar.YEAR) != calendar1.get(Calendar.YEAR)) {
            return transferLongToDate(VCare.get().getString(R.string.string_year_month_day_h_m), createTime);
        }

        return "";
    }


    /*
     * 毫秒转化天时分秒毫秒
     * 格式1天12小时12分12秒
     */
    public static String formatTime(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        StringBuffer sb = new StringBuffer();
        if (day > 0) {
            sb.append(day + VCare.get().getString(R.string.tian));
        }
        if (hour > 0) {
            sb.append(hour + VCare.get().getString(R.string.xiaoshi));
        }
        if (minute > 0) {
            sb.append(minute + VCare.get().getString(R.string.fen));
        }
        if (second > 0) {
            sb.append(second + VCare.get().getString(R.string.miao));
        }
//            if(milliSecond > 0) {
//                sb.append(milliSecond+"毫秒");
//            }
        return sb.toString();
    }

    public static String longToHMS(long mm) {
        long time = mm / 1000L;
        String timeStr = null;
        long hour = 0;
        long minute = 0;
        long second = 0;
        if (time <= 0)
            return "00:00";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
//                if (hour > 99)
//                    return "99:59:59";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }

    public static String unitFormat(long i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Long.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }

    /**
     * 把毫秒转化成日期
     *
     * @param dateFormat(日期格式，例如：MM/ dd/yyyy HH:mm:ss)
     */
    public static String transferLongToDate(String dateFormat, Long millSec) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        Date date = new Date(millSec);
        return sdf.format(date);
    }

    /**
     * floa转double防止因为补位而导致精度损失
     */
    public static double float2double(float f) {
        return Double.parseDouble(Float.toString(f));
    }

    /**
     * 温度转换C -> F
     */
    public static float c2F(float f) {
        BigDecimal b1 = new BigDecimal(f * 1.8f + 32F);
        float f1 = b1.setScale(2, BigDecimal.ROUND_DOWN).floatValue();
        return f1;
    }

    /**
     * 温度转换C -> F
     */
    public static float c2FHalfDown(float f) {
        BigDecimal b1 = new BigDecimal(f * 1.8f + 32F);
        float f1 = b1.setScale(1, BigDecimal.ROUND_HALF_DOWN).floatValue();
        return f1;
    }

    public static float f2C(float f) {
        BigDecimal b1 = new BigDecimal((f - 32F) / 1.8F);
        float f1 = b1.setScale(2, BigDecimal.ROUND_DOWN).floatValue();
        return f1;
    }

    public static float f2CHalfDown(float f) {
        BigDecimal b1 = new BigDecimal((f - 32F) / 1.8F);
        float f1 = b1.setScale(1, BigDecimal.ROUND_HALF_DOWN).floatValue();
        return f1;
    }
}
